<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="syncAllTable"
        >整库表增量同步</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="resyncTable"
        >表重新同步</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleGenTable"
        >代码生成</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据源主键" align="center" prop="tableId" />
      <el-table-column label="表名称" align="center" prop="tableName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/tool/cusGen/tableColumn/' + scope.row.tableId" class="link-type">
            <span>{{ scope.row.tableName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="所属模块" align="center" prop="moduleName" />
      <el-table-column label="日志功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.logFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="鉴权功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.authFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="查询功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.searchFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="分页功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.pageFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="列表功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.listFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="详情功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.detailFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="导出功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.exportFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="添加功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.insertFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="修改功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.updateFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="删除功能">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_open_status" :value="scope.row.deleteFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="数据源备注" align="center" prop="remarks" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-refresh"
            @click="resyncTable(scope.row)"
          >同步</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-download"
            @click="handleGenTable(scope.row)"
          >生成代码</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 预览界面 -->
    <el-dialog :title="preview.title" :visible.sync="preview.open" width="80%" top="5vh" append-to-body class="scrollbar">
      <el-tabs v-model="preview.type" @tab-click="switchTab">
        <el-tab-pane
          v-for="(sysEnum, key) in sysEnum.genResouceTypeTemplate"
          :label="sysEnum.describStr"
          :name="sysEnum.code + ''"
          :key="key"
        >
          <el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="preview.data" v-clipboard:success="clipboardSuccess" style="float:right">复制</el-link>
          <pre><code class="hljs">{{preview.data}}</code></pre>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import {listTableList, syncAllTable, resyncTable, previewTable, getGenInfo , deleteTableByIds} from "@/api/tool/cusGen";
import sysEnumData from "@/components/SysEnumData";

export default {
  name: "Config",
  dicts: ['sys_normal_open_status'],
  computed: {
    sysEnumData() {
      return sysEnumData
    }
  },
  sysEnums:['genResouceTypeTemplate'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      tableIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 参数表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        dbId: undefined,
        tableName: undefined
      },
      // 表单校验
      rules: {
        dsName: [
          { required: true, message: "数据源名称不能为空", trigger: "blur" }
        ],
        dbName: [
          { required: true, message: "数据库名称不能为空", trigger: "blur" }
        ]
      },
      // 预览参数
      preview: {
        open: false,
        title: "代码预览",
        tableId: undefined,
        type: "1",
        data: {}
      },
      packageInfo:{}
    };
  },
  created() {
    const dbId = this.$route.params && this.$route.params.dbId;
    this.queryParams.dbId = dbId

    //加载基础包信息
    getGenInfo().then(res => {
      this.packageInfo = res
    });

    this.getList();
  },
  methods: {
    /** 查询数据源列表 */
    getList() {
      this.loading = true;
      listTableList(this.queryParams).then(res => {
          this.tableList = res;
          this.loading = false;
        }
      );
    },
    /** 表删除 */
    /** 删除按钮操作 */
    handleDelete(row) {
      const tableIds = row.tableId || this.tableIds;
      this.$modal.confirm('是否确认删除表？').then(function() {
        return deleteTableByIds(tableIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 表单重置
    reset() {
      this.form.tableName = undefined
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //代码预览列表tab切换
    switchTab(tab){
      previewTable({
        tableId: this.preview.tableId,
        type: this.preview.type
      }).then(res => {
        this.$nextTick(()=>{
          this.preview.data = res;
        })
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.tableIds = selection.map(item => item.tableId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 复制代码成功 */
    clipboardSuccess() {
      this.$modal.msgSuccess("复制成功");
    },
    //全表同步
    syncAllTable(){
      var that = this
      this.$modal.confirm('是否确认增量同步数据库？').then(function() {
        console.log("====")
        that.loading = true;
        that.getList();
        return syncAllTable(that.queryParams.dbId);
      }).then(() => {
        console.log("=======")
        that.getList();
        that.$modal.msgSuccess("增量同步成功");
      }).catch(() => {})
        .finally(()=>{
          that.loading = false
        })
    },
    //单表同步
    resyncTable(row){
      const tableIds = row.tableId || this.tableIds;
      this.$modal.confirm('是否确认同步表参数编号为"' + tableIds + '"的数据项？').then(function() {
        return resyncTable(tableIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("同步成功");
      }).catch(() => {});
    },
    /** 预览按钮 */
    handlePreview(row) {
      this.preview.tableId = row.tableId
      previewTable({
        tableId:row.tableId,
        type: "1"
      }).then(res => {
        this.$nextTick(()=>{
          this.preview.data = res;
          this.preview.open = true;
        })
      });
    },
    /** 生成代码操作 */
    handleGenTable(row) {
      const tableIds = row.tableId || this.tableIds;
      if (tableIds == "") {
        this.$modal.msgError("请选择要生成的数据");
        return;
      }
      this.download('/generate/table/generateTable', {
        tableIds: tableIds
      }, `代码生成_${new Date().getTime()}.zip`)
    },
  }
};
</script>
